package zpe.jsf.view.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import zpe.genericcrud.jsf.view.bean.AbstractCrudBean;
import zpe.model.Agendamento;
import zpe.model.Cliente;
import zpe.model.enums.TipoMovimentacao;
import zpe.model.enums.TipoRequisicao;
import zpe.service.AgendamentoService;

@ManagedBean(name = "agendamentoBean")
@ViewScoped
public class AgendamentoBean extends AbstractCrudBean<Agendamento, AgendamentoService> {

	@ManagedProperty("#{agendamentoService}")
	protected AgendamentoService service;
	
	private DataModel<Cliente> dmPesquisaProprietario = new ListDataModel<Cliente>();
	
	@SuppressWarnings("unchecked")
	public void adicionaProprietario(ActionEvent event){
		List<Cliente> l = (List<Cliente>)getDmPesquisaProprietario().getWrappedData();
		if(l!=null){
			l.add(getEntity().getProprietario());
		} else {
			l = new ArrayList<Cliente>();
			l.add(getEntity().getProprietario());
		}
		getDmPesquisaProprietario().setWrappedData(l);
		email = new Cliente(getEntity());
	}
	
	@SuppressWarnings("unchecked")
	public void removeProprietario(ActionEvent event){
		List<Cliente> l = (List<Cliente>)getDmPesquisaProprietario().getWrappedData();
		l.remove(getDmPesquisaProprietario().getRowIndex());
		setDmPesquisaProprietario(new ListDataModel<Cliente>());
		getDmPesquisaProprietario().setWrappedData(l);
	}
	
	public List<SelectItem> getTiposRequisicao() {

		List<SelectItem> itens = new ArrayList<SelectItem>(0);

		for (TipoRequisicao item : TipoRequisicao.values())
			itens.add(new SelectItem(item));

		return itens;

	}
	
	public List<SelectItem> getTiposMovimentacao() {

		List<SelectItem> itens = new ArrayList<SelectItem>(0);

		for (TipoMovimentacao item : TipoMovimentacao.values())
			itens.add(new SelectItem(item));

		return itens;

	}

	public DataModel<Cliente> getDmPesquisaProprietario() {
		return dmPesquisaProprietario;
	}

	public void setDmPesquisaProprietario(DataModel<Cliente> dmPesquisaProprietario) {
		this.dmPesquisaProprietario = dmPesquisaProprietario;
	}

}
